package cc.autoapi.pucong.auto.flow.core.flow.execNode.task.system;


import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.node.ExecNode;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.node.ExecParamIn;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.node.NodeParamIn;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.node.NodeParamOut;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.exception.AutoDesignException;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.selfenum.ParamPositionEnum;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.task.AbstractTask;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ApiConfigTask extends AbstractTask {

    private List<NodeParamIn> paramsIns;
    private List<NodeParamOut> paramsOuts;

    public ApiConfigTask() {
        paramsIns = new ArrayList<>();

        paramsOuts = new ArrayList<>();
    }

    @Override
    public void run() {
        Map<String, Object> queryParams = getQueryParams();
        Map<String, Object> bodyParamMap = getBodyParamMap();
        Map<String, String> headerParamMap = getHeaderParamMap();
        ExecNode execNode = getExecNode();
        List<ExecParamIn> execParamInList = execNode.getParamInList();
        for (ExecParamIn execParamIn : execParamInList) {
            String paramCode = execParamIn.getParamCode();
            Boolean needValue = execParamIn.getNeedValue();
            ParamPositionEnum paramPosition = execParamIn.getParamPosition();
            Object value = null;
            switch (paramPosition) {
                case QUERY:
                    value = queryParams.get(paramCode);
                    break;
                case BODY:
                    value = bodyParamMap.get(paramCode);
                    break;
                case HEADER:
                    value = headerParamMap.get(paramCode);
                    break;
                default:
                    throw new AutoDesignException("传入参数位置错误");
            }
            if (value == null && needValue) {
                throw new AutoDesignException("API必填参数为空：" + paramCode);
            }
            if (value != null && needValue && value instanceof String && StringUtils.isBlank(value.toString())) {
                throw new AutoDesignException("API必填参数为空：" + paramCode);
            }
            execParamIn.setDateValue(value);
        }
    }

    @Override
    public String getTaskName() {
        return "api配置节点";
    }

    @Override
    public String getTaskDesc() {
        return null;
    }

    @Override
    public Boolean getTaskParamInAuto() {
        return true;
    }

    @Override
    public String getTaskSvgIcon() {
        return "<svg t=\"1728283839454\" class=\"icon\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"9272\" width=\"32\" height=\"32\"><path d=\"M475.249778 690.176h-48.355556l-19.228444-49.948444h-87.836445l-18.204444 49.948444h-47.104l85.674667-219.818667H386.844444zM393.443556 603.022222l-30.264889-81.578666L333.482667 603.022222zM498.801778 690.176V470.357333h71.224889a252.814222 252.814222 0 0 1 52.792889 3.299556 57.685333 57.685333 0 0 1 31.630222 21.504 68.266667 68.266667 0 0 1 12.743111 42.780444 69.973333 69.973333 0 0 1-7.395556 34.133334 59.392 59.392 0 0 1-18.659555 21.731555 63.943111 63.943111 0 0 1-22.755556 10.467556 259.640889 259.640889 0 0 1-45.511111 3.072h-29.696v82.944z m44.373333-182.044444v62.350222h24.234667a116.394667 116.394667 0 0 0 35.157333-3.413334 29.354667 29.354667 0 0 0 13.880889-10.808888 29.809778 29.809778 0 0 0 5.006222-17.066667A28.330667 28.330667 0 0 0 614.4 518.826667a30.378667 30.378667 0 0 0-17.863111-9.784889 210.375111 210.375111 0 0 0-31.971556-1.479111zM702.350222 690.176V470.357333h44.373334v219.818667z\" fill=\"#C0D043\" p-id=\"9273\"></path><path d=\"M761.173333 832.853333H525.880889v-52.337777H761.173333c67.697778 0 121.628444-29.696 152.007111-83.740445a205.368889 205.368889 0 0 0-3.868444-199.793778c-36.408889-59.847111-102.4-91.022222-186.254222-88.405333l-26.965334 1.024v-27.192889c0-56.888889-19.000889-104.334222-54.727111-136.533333a181.248 181.248 0 0 0-122.311111-44.487111c-88.405333 0-187.278222 56.888889-187.278222 181.248V409.6l-27.534222-1.479111c-85.219556-4.323556-152.348444 25.941333-189.44 85.333333a198.428444 198.428444 0 0 0-4.664889 196.608c32.199111 58.140444 91.022222 90.112 166.115555 90.112h95.004445v52.337778h-95.004445c-93.411556 0-170.666667-42.666667-211.854222-116.963556a251.676444 251.676444 0 0 1 6.030222-250.311111c43.576889-69.745778 117.532444-108.316444 210.261334-110.250666 12.856889-135.168 128.113778-206.165333 238.364444-206.165334a233.472 233.472 0 0 1 157.468445 58.026667 222.549333 222.549333 0 0 1 70.769777 149.390222c91.022222 3.527111 164.067556 43.349333 206.734223 113.777778a255.544889 255.544889 0 0 1 4.778666 252.586667c-39.480889 69.973333-111.502222 110.250667-197.632 110.250666z\" fill=\"#C0D043\" p-id=\"9274\"></path><path d=\"M486.058667 806.912l91.022222 68.266667v-136.533334l-91.022222 68.266667z\" fill=\"#C0D043\" p-id=\"9275\"></path></svg>";
    }

    @Override
    public List<NodeParamIn> getPropertyParamsIns() {
        return paramsIns;
    }

    @Override
    public List<NodeParamOut> getPropertyParamsOuts() {
        return paramsOuts;
    }
}
